<template>
  <div class="userlist">
    <el-button type="primary" @click="adddialogVisible = true">
      新增用户
    </el-button>
    <el-table :data="userlist">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column label="用户名" prop="username"></el-table-column>
      <el-table-column label="密码" prop="password"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="showedit(scope.row._id)"
            size="mini"
            icon="el-icon-edit"
          >
            编辑
          </el-button>
          <el-button
            type="danger"
            @click="del(scope.row._id)"
            size="mini"
            icon="el-icon-delete"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增用户对话框 -->
    <el-dialog title="新增用户" :visible.sync="adddialogVisible" width="50%">
      <el-form :model="editForm">
        <el-form-item label="用户名">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="用户密码">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addNewUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改对话框 -->
    <el-dialog
      title="编辑用户信息"
      :visible.sync="editdialogVisible"
      width="50%"
    >
      <el-form :model="editForm">
        <el-form-item label="用户名">
          <el-input v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="用户密码">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  GetUserList,
  DeleteUser,
  GetUserListById,
  UpdateUserListById,
  AddUsersList,
} from '@/api/index'
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      userlist: [],
      editdialogVisible: false,
      adddialogVisible: false,
      addForm: {
        username: '',
        password: '',
      },
      editForm: {
        username: '',
        password: '',
      },
      id: '',
    }
  },
  methods: {
    ...mapMutations(['getUsersNum']),

    // 新增用户
    async addNewUser() {
      const { data: res } = await AddUsersList(this.addForm)
      console.log(res, 'add')
      this.getuserlist()
      this.adddialogVisible = false
    },
    // 查询
    async getuserlist() {
      const { data: res } = await GetUserList()
      console.log(res)
      this.userlist = res
      console.log(res.length,'000--00')
      this.getUsersNum(res.length)
    },
    // id查询
    async showedit(id) {
      this.editdialogVisible = true
      console.log(id, '0')
      const { data: res } = await GetUserListById(id)
      console.log(res)
      this.editForm = res.data
      console.log(this.editForm)
      this.id = id
    },

    // 删除
    async del(id) {
      console.log(id, '11')
      const { data: res } = await DeleteUser(id)
      console.log(res)
      this.getuserlist()
    },
    async edit() {
      const data = {
        id: this.id,
        info: this.editForm,
      }
      console.log(this.id, '------')
      const { data: res } = await UpdateUserListById(this.id, data)
      console.log(res)
      this.getuserlist()

      this.editdialogVisible = false
    },
  },
  created() {
    this.getuserlist()
    // this.edit()
  },
}
</script>

<style lang="less" scoped></style>
